package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FaUserBlacklistDTO;
import com.financing.platform.service.IFaUserBlacklistService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.vo.FaUserBlacklistVO;

/**
 * FA用户黑名单
 * @author lisu
 *
 */
@Controller("faUserBlacklistActionApi")
@RequestMapping("/api/blacklist")
@Scope("prototype")
public class FAUserBlacklistAction extends BaseAction{
	
	@Autowired
	private IFaUserBlacklistService faUserBlacklistServiceImpl;

	
	@RequestMapping("/addBlacklist.shtml")
	@ResponseBody
	public ResultMap addBlacklist(HttpServletRequest request,
			HttpServletResponse response,FaUserBlacklistDTO faUserBlacklistDTO) {
		try{
			log.info("in the addBlacklist begin!");
			
			DTOdateSet.setCreateInfo(faUserBlacklistDTO, request.getSession());
			DTOdateSet.setUpdateInfo(faUserBlacklistDTO, request.getSession());
			
			faUserBlacklistServiceImpl.addFaUserBlacklist(faUserBlacklistDTO);
			
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("in the addBlacklist end!");
		} catch(Exception e){
			
			log.error("in the addBlacklist error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/queryBlacklist.shtml")
	@ResponseBody
	public ResultMap queryBlacklist(HttpServletRequest request,
			HttpServletResponse response,FaUserBlacklistVO faUserBlacklistVO ,Pager pager) {
		try{
			log.info("in the queryBlacklist begin!");
			
			faUserBlacklistVO.setBegin(pager.getStartIndex());
			faUserBlacklistVO.setPage(pager.getPageNo());
			faUserBlacklistVO.setPageSize(pager.getPageSize());
			
			int count = faUserBlacklistServiceImpl.queryFaUserBlacklistCount(faUserBlacklistVO);
			pager.setTotalCount(count);
			
			List<FaUserBlacklistVO> list;
			
			if(count>0){
				list = faUserBlacklistServiceImpl.queryFaUserBlacklist(faUserBlacklistVO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", pager);
			
			log.info("in the queryBlacklist end!");
		} catch(Exception e){
			
			log.error("in the queryBlacklist error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	@RequestMapping("/queryBlacklistDetail.shtml")
	@ResponseBody
	public ResultMap queryDetail(HttpServletRequest request,
			HttpServletResponse response,FaUserBlacklistVO faUserBlacklistVO,Pager pager) {
		try{
			log.info("in the queryBlackDetail begin!");
			
			if(faUserBlacklistVO.getUserId() == null){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			List<FaUserBlacklistVO> list = faUserBlacklistServiceImpl.queryFaUserBlacklist(faUserBlacklistVO);
			
			FaUserBlacklistVO bean;
			
			if(list == null || list.size() <= 0){
				bean = null;
			}else{
				bean = list.get(0);
			}
			
			List<FaUserBlacklistDTO> listBlack = faUserBlacklistServiceImpl.queryBlacklistByUserId(faUserBlacklistVO);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("dto", bean);
			resultMap.setAttribute("list", listBlack);
			
			log.info("in the queryBlackDetail end!");
		} catch(Exception e){
			
			log.error("in the queryBlackDetail error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
}
